// Dialog.h: interface for the CSIPDialog class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_DIALOG_H__87A2F2C0_153C_4B29_AF04_18FCD16F4CAD__INCLUDED_)
#define AFX_DIALOG_H__87A2F2C0_153C_4B29_AF04_18FCD16F4CAD__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include "SIPHeader.h"
#include "SIPMessage.h"
#include "Transport.h"
#include "SDPContent.h"
//#include "SIPSession.h"
#include "base.h"

#define CLEARLIST(l,t) t *pt;while(l.empty() == false){pt = l.front();l.pop_front();delete pt;};
#define COPYLIST(s,d,t) list<t*>::const_iterator i;for(i=s.begin();i!=s.end();i++){d.push_back(new t(**i));}

class BASE_API CSIPDialog  
{
public:
	enum State{
		early,
		confirmed,
		completed,
		terminated
	};	
	CSIPDialog(const CSIPNameAddr &contact);
	virtual ~CSIPDialog();
	bool CreateSession(const CSDPContent *psdp);
	bool ModifySession(const CSDPContent *psdp);
	void SetConfirm(){m_eState = confirmed;};
	bool IsCreated(){return m_bCreated;};
	void Clear();
	bool IsTerminated(){return m_eState == terminated;};
	bool IsConfirmed(){return m_eState == confirmed;};	
	State GetState(){return m_eState;};
	mystring GetRemoteTag(){return m_sRemoteTag;};
	mystring GetLocalTag(){return m_sLocalTag;};
	void SetRequestMessage(CSIPMessage &request);
	void CreateDialogAsUAC(const CSIPMessage& response);
	void TargetRefreshResponse(const CSIPMessage& response);
	int TargetRefreshRequest(const CSIPMessage& request);
	CSIPUri &GetRemoteTarget(){return m_RemoteTarget;};
	CSIPMessage* MakeRequest(MethodTypes method);
	CSIPMessage* MakeResponse(const CSIPMessage &request,int code);
	CSIPMessage* MakeResponse(int code);
	CSIPMessage* MakeInitialRegister(const CSIPNameAddr& registrar,
                            const CSIPNameAddr& aor);
	
	CSIPMessage*  MakeInitialSubscribe(const CSIPUri &requesturi,const CSIPNameAddr& target, 
                             const CSIPNameAddr& from);
	CSIPMessage* MakeInitialInvite(const CSIPNameAddr& target,
                          const CSIPNameAddr& from);
	CSIPMessage* MakeInitialINFO(const CSIPNameAddr& target,
                          const CSIPNameAddr& from);
	
	CSIPMessage* MakeInvite();
	CSIPMessage* MakeRegister();
	CSIPMessage* MakeSubscribe();
	CSIPMessage* MakePublish();
	CSIPMessage* MakeINFO();
	CSIPMessage* MakeBye();
	CSIPMessage* MakeRefer(const CSIPNameAddr& referTo);
	CSIPMessage* MakeNotify();
	CSIPMessage* MakeOptions();
	CSIPMessage* MakeAck();//const CSIPMessage& original);
	CSIPMessage* MakeCancel(const CSIPMessage& request);
	CSIPCallID & GetCallId(){return m_CallID;};
	CSDPContent &GetSDPContent(){return m_SDPContent;};
//	mystring GetCallId();
//	CSIPSDPContent::Session &GetSession(){return m_Session;};
	CSIPMessage* GetRequestMessage(){return m_pRequestMsg;};

	CSIPUri &GetRemoteUri(){return m_RemoteUri;};
//DEL 	void SetContact(CSIPNameAddr &na){m_Contact = na;};
	uint32	m_nTimer;
	uint32	m_nInterval;
	mystring m_strChannelID;
	uint32	m_nNextTime;
	uint32	m_nExpires;
	int		m_nResponceCode; // added by liuhf 2008.7.22
	int		m_nByeFlag;  //added by wjy 20100122

protected:
	void IncrementCSeq(CSIPMessage& request);
	void CopyCSeq(CSIPMessage& request);
	CSIPCallID MakeReplaces();

protected:
	CSIPCallID				m_CallID;
	unsigned long			m_nLocalSequence;
	unsigned long			m_nRemoteSequence;
	State					m_eState;
	bool					m_bCreated;
	bool					m_bRemoteEmpty;
	bool					m_bLocalEmpty;
	bool					m_bSecure;
	mystring				m_sRemoteTag;
	mystring				m_sLocalTag;
	CSIPUri					m_RemoteUri;
	CSIPUri					m_LocalUri;
	CSIPUri					m_RemoteTarget;
	mystring				m_sEvent;
	CSIPNameAddr			m_Contact;
	CSIPNameAddr			m_Registrar;
	CSIPNameAddr			m_Subscriber;
	list<CSIPRoute*>		m_RouteList;
	CTransport::Tuple		m_Destination;
	CTransport::Tuple		m_Source;
	CSIPMessage*			m_pRequestMsg;
	//CSIPSession*			m_pSession;
	CSDPContent				m_SDPContent;
	
};

#endif // !defined(AFX_DIALOG_H__87A2F2C0_153C_4B29_AF04_18FCD16F4CAD__INCLUDED_)
